const carousel = document.querySelector('.news-carousel');
const images = carousel.querySelectorAll('.news-image');
const dots = carousel.querySelectorAll('.dot');
const prevBtn = carousel.querySelector('.prev');
const nextBtn = carousel.querySelector('.next');
let currentIndex = 0;

function showImage(index) {
  images.forEach(img => img.classList.remove('active'));
  dots.forEach(dot => dot.classList.remove('active'));
  images[index].classList.add('active');
  dots[index].classList.add('active');
}

function nextImage() {
  currentIndex = (currentIndex + 1) % images.length;
  showImage(currentIndex);
}

function prevImage() {
  currentIndex = (currentIndex - 1 + images.length) % images.length;
  showImage(currentIndex);
}

// 自动轮播
let timer = setInterval(nextImage, 3000);

// 鼠标悬停时暂停轮播
carousel.addEventListener('mouseenter', () => clearInterval(timer));
carousel.addEventListener('mouseleave', () => timer = setInterval(nextImage, 3000));

// 按钮点击事件
prevBtn.addEventListener('click', prevImage);
nextBtn.addEventListener('click', nextImage);

// 点击圆点切换
dots.forEach((dot, index) => {
  dot.addEventListener('click', () => {
    currentIndex = index;
    showImage(currentIndex);
  });
});

// Hero Carousel
document.addEventListener('DOMContentLoaded', function() {
  const slides = document.querySelectorAll('.hero-slide');
  const dots = document.querySelectorAll('.hero-dot');
  const prevBtn = document.querySelector('.hero-btn.prev');
  const nextBtn = document.querySelector('.hero-btn.next');
  let currentSlide = 0;
  let slideInterval;

  function showSlide(index) {
    // Remove active class from all slides and dots
    slides.forEach(slide => slide.classList.remove('active'));
    dots.forEach(dot => dot.classList.remove('active'));
    
    // Add active class to current slide and dot
    slides[index].classList.add('active');
    dots[index].classList.add('active');
    
    currentSlide = index;
  }

  function nextSlide() {
    let next = currentSlide + 1;
    if (next >= slides.length) {
      next = 0;
    }
    showSlide(next);
  }

  function prevSlide() {
    let prev = currentSlide - 1;
    if (prev < 0) {
      prev = slides.length - 1;
    }
    showSlide(prev);
  }

  // Event listeners for buttons
  nextBtn.addEventListener('click', () => {
    nextSlide();
    resetInterval();
  });

  prevBtn.addEventListener('click', () => {
    prevSlide();
    resetInterval();
  });

  // Event listeners for dots
  dots.forEach((dot, index) => {
    dot.addEventListener('click', () => {
      showSlide(index);
      resetInterval();
    });
  });

  // Auto slide
  function startInterval() {
    slideInterval = setInterval(nextSlide, 3000); // Change slide every 5 seconds
  }

  function resetInterval() {
    clearInterval(slideInterval);
    startInterval();
  }

  // Start auto sliding
  startInterval();

  // Pause on hover
  const heroCarousel = document.querySelector('.hero-carousel');
  heroCarousel.addEventListener('mouseenter', () => {
    clearInterval(slideInterval);
  });

  heroCarousel.addEventListener('mouseleave', () => {
    startInterval();
  });
});

window.onload = function () {
  $('.news-list .news-tabs .news-tab').hover(function () {
    var id = $(this).data('id');
    $(".news-items .".concat(id)).animate({
      'opacity': 1
    }, 0).show();
    $(".news-items .".concat(id)).siblings().animate({
      'opacity': 0
    }, 0).hide();
    $(this).addClass('active').siblings().removeClass('active');
  });
}

window.onload = function () {
  // 用于保存上次点击的菜单元素
  let lastClickedMenu = null;

  $('.index_customer .index_customer_type .customer_menus .customer_menu').hover(function () {
    var id = $(this).data('id');
    $(".customer_introduction .".concat(id)).animate({
      'opacity': 1
    }, 0).show();
    $(".customer_introduction .".concat(id)).siblings().animate({
      'opacity': 0
    }, 0).hide();
    $(this).addClass('active').siblings().removeClass('active');
    debugger
    lastClickedMenu = this;
  })

  // // hover事件
  // $('.index_customer .index_customer_type .customer_menus .customer_menu').hover(
  //   function () {
  //     if (lastClickedMenu && this !== lastClickedMenu) {
  //       $('.index_customer .index_customer_type .customer_menus .customer_menu').removeClass('active');
  //       $(this).addClass('active');
  //       $(lastClickedMenu).addClass('active');
  //     }
  //   }
  // );
}